package model.game;

import java.io.Serializable;
import java.util.ArrayList;

public class Place implements Location, Serializable{
    private int id;
    protected String name;
    protected ArrayList<Player> players;
    protected ArrayList<Player> npcs;
    protected ArrayList<Item> items;
    public Place(String name){
        this.name = name;
        players = new ArrayList<Player>();
        npcs = new ArrayList<Player>();
        items = new ArrayList<Item>();
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setName(String name) {
        this.name = name;
    }

    @Override
    public ArrayList<Player> getPlayers() {
        return players;
    }
    @Override
    public ArrayList<Player> getPerson() {
        ArrayList<Player> persons = new ArrayList<Player>();
        for (int i=0; i<players.size(); i++){
            persons.add(players.get(i));
        }
        for (int i=0; i<npcs.size(); i++){
            persons.add(npcs.get(i));
        }
        return persons;
    }
    @Override
    public void addPlayer(Player p) {
        players.add(p);
    }

    @Override
    public void removePlayer(Player p) {
        players.remove(p);
    }
    @Override
    public Player findPlayer(String name) {
        for (int i = 0; i < players.size(); i++) {
            if (players.get(i).getName().equals(name)) {
                return players.get(i);
            }
        }
        return null;
    }
    @Override
    public Player findNPC(String name) {
        for (int i = 0; i < npcs.size(); i++) {
            if (npcs.get(i).getName().equals(name)) {
                return npcs.get(i);
            }
        }
        return null;
    }
    
    @Override
    public Location whereIsPlayer(String name){
        for (int i=0; i< players.size(); i++){
            if (players.get(i).getName().equals(name)){
                return this;
            }
        }
        return null;
    }
    @Override
    public int getTotalPlayers(){
        int count = 0;
        count = players.size();
        return count;
    }
    @Override
    public boolean playerExists(String name) {
        for (int i = 0; i < players.size(); i++) {
            if (players.get(i).getName().equals(name)) {
                return true;
            }
        }
        return false;
    }
    @Override
    public Location findLocation(String name){
        if (this.name.equals(name)){
            return this;
        }else{
            return null;
        }
    }

    public void setPlayers(ArrayList<Player> players) {
        this.players = players;
    }

    @Override
    public ArrayList<Player> getNpcs() {
        return npcs;
    }

    public void setNpcs(ArrayList<Player> npcs) {
        this.npcs = npcs;
    }

    @Override
    public ArrayList<Item> getItems() {
        return items;
    }

    public void setItems(ArrayList<Item> items) {
        this.items = items;
    }

    @Override
    public void addItem(Item i) {
        items.add(i);
    }

    @Override
    public void removeItem(Item i) {
        items.remove(i);
    }
    
    public void removeItem(String name){
        for (int i=0; i<items.size(); i++){
            if (items.get(i).getName().equals(name)){
                items.remove(i);
                break;
            }
        }
    }
}
